package com.summer.servlet.user;

import com.summer.pojo.User;
import com.summer.service.user.UserServiceImpl;
import com.summer.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者:夏天
 * qq:1205232048
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从前端请求得到用户名 和密码
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");

        //去调用业务层 业务层会去调用
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);
        if(user != null){
            //将用户的信息存入session中
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            //成功则跳转到frame.jsp
            resp.sendRedirect("jsp/frame.jsp");
        }else {
            //设置一个节点   可以在前端jsp文件通过${error}去获取 "用户名或者密码不正确"
            req.setAttribute("error","用户名或者密码不正确");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
